/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package suche;
import java.sql.*;
import java.util.Vector;
import java.io.*;
import java.util.Vector;

/**
 *
 * @author Gatscha
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws Exception{

   Class.forName("com.mysql.jdbc.Driver");
   Connection verb = DriverManager.getConnection("jdbc:derby: usr_web26_1;create=true");
    Statement stmt = verb.createStatement();
  //private serverConnect connect;

   //1.Nutzer auslesen
   int User_ID;
   String query = "Select user_id from User where user_email = 'gatscha84@yahoo.de'";////!!!!!!!!!!!!!!!!!
   ResultSet rs = stmt.executeQuery( query );
   User_ID = Integer.parseInt(rs.getNString(1));

   //2.Keywords auslesen
  String[] Keywords = new String[10];
  int q = 0;

  rs = stmt.executeQuery( "SELECT key_one, key_two, key_three, key_four, key_five, key_six,  key_seven,key_eight, key_nine, key_ten FROM Keywords where user_id = '" +User_ID+"'"); //erstmal nur das erste?

  for(int i = 0; i < 10; i++)
  {
      String test = rs.getString(i+1);
      
      if(isNullOrEmpty(test)==false)
      {
          Keywords[q] = rs.getString(i+1);
          q++;
      }
    
  }


  //3. Feeds des Users suchen, Feed selektieren von Anbieter (Datum: today - 28 Tage)

   rs = stmt.executeQuery( "SELECT Count(id) FROM RSS join RSS_List on RSS.RSS_List_ID = RSS_List.id where user_id = '" +User_ID+"' And DATE_SUB(CURDATE(),INTERVAL 28 DAY) <= rss_date;");
   int anz = Integer.parseInt(rs.getString(1));
   int[][] sammeln = new int[anz][2];

   rs = stmt.executeQuery( "SELECT id FROM RSS join RSS_List on RSS.RSS_List_ID = RSS_List.id where user_id = '" +User_ID+"' And DATE_SUB(CURDATE(),INTERVAL 28 DAY) <= rss_date;");
    int j = 0;
    while ( rs.next() )
    {
      sammeln[j][0] = Integer.parseInt(rs.getNString(j+1));
      j++;
    }


    //4. Feeds öffnen, durchsuchen, schließen
int count = 0;

  for(int i = 0; i < anz; i++)
  {
        String desc = "";        
        int dings = sammeln[i][0];
    rs = stmt.executeQuery("Select description from RSS where rss_id = '"+ dings+"'");

    while ( rs.next() )
    {
        desc = rs.getString(1);
      }
    
    int pos = desc.indexOf(Keywords[i]);
    while (pos != -1) {
      count++;
      pos = desc.indexOf(Keywords[i],++pos);

    if (pos == -1)
    {
       sammeln[i][1] = count;
       break;
    }
   }
}

    //Kopie anlegen
                int[][] kopie = new int[2][count];

                for(int i = 0; i < count; i++)
                {
                    kopie[0][i] = sammeln [0][i];
                    kopie[1][i] = sammeln [1][i];
                }


		quickSort (sammeln[1]);

                for(int i = 0; i < count; i++)
                {
                    int hilf = sammeln[1][i];
                    int hilf2 = -1;
                    int hilf3 = 0;

                    while(hilf2 == -1)
                    {
                         if(kopie[1][hilf3] == hilf)
                         {
                            sammeln[0][i] = kopie[0][hilf3];
                            kopie[0][hilf3] = -1;
                            kopie[1][hilf3] = -1;
                            hilf2 = hilf3;
                         }
                         hilf3++;
                    }
                }
    }


    //Sortierung
static void swap (int[] array, int idx1, int idx2) {
		int tmp = array[idx1];
		array[idx1] = array[idx2];
		array[idx2] = tmp;
	}


	/*
	 * Implementierung des QuickSort
	 */

	// Hilfsmethode f�r rekursives Sortieren
	static void qsort (int[] array, int le, int ri) {
		int lo = le, hi = ri;

		if (hi > lo) {
			// Pivotelement bestimmen
			int mid = array[(lo + hi) / 2];
			while (lo <= hi) {
				// Erstes Element suchen, das gr��er oder gleich dem
				// Pivotelement ist, beginnend vom linken Index
				while (lo < ri && array[lo] < mid)
					++lo;

				// Element suchen, das kleiner oder gleich dem
				// Pivotelement ist, beginnend vom rechten Index
				while (hi > le && array[hi] > mid)
					--hi;

				// Wenn Indexe nicht gekreuzt --> Inhalte vertauschen
				if (lo <= hi) {
					swap(array, lo, hi);
					++lo;
					--hi;
				}
			}
			// Linke Partition sortieren
			if (le < hi) {
				qsort (array, le, hi);
			}

			// Rechte Partition sortieren
			if (lo < ri) {
				qsort( array, lo, ri);
			}
		}
	}

	static void quickSort (int[] array) {
		qsort (array, 0, array.length - 1);
	}
///

public static boolean isNullOrEmpty( String str )
{
  return str == null || str.length() == 0;
}    

}


